package cate.common.table.guild.task;

import cate.common.table.ESheet;
import cate.common.util.random.RWList;

import java.util.HashMap;
import java.util.Map;

public class GuildTaskWeightSheet extends ESheet<GuildTaskWeightRow> {
    @Override
    public String getSheetName() {
        return "内政任务刷新权重";
    }

    private Map<Integer, RWList<GuildTaskWeightRow>> groupMap;

    public void onLoaded() {
        groupMap = new HashMap<>();
        for (GuildTaskWeightRow row : this.list) {
            RWList<GuildTaskWeightRow> rwl = groupMap.get(row.group);
            if (rwl == null) {
                rwl = new RWList<>();
                groupMap.put(row.group, rwl);
            }
            rwl.add(row);
        }
        groupMap.values().forEach(RWList::setup);
    }

    public int randomTask(int groupId) {
        RWList<GuildTaskWeightRow> rwl = groupMap.get(groupId);
        if (rwl == null) {
            return -1;
        }
        GuildTaskWeightRow row = rwl.get();
        if (row == null) {
            return -1;
        }
        return row.taskId;
    }
}
